#include "Hud.h"
#include "Engine.h"

Hud::Hud(){
	m_pgeode = new osg::Geode();
	popupTxt = new osgText::Text;

	initCam();
	drawBackground();

	// turn lighting off for the text and disable depth test to ensure it's always ontop.
	osg::StateSet* stateset = m_pgeode->getOrCreateStateSet();
	stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);

	// Remplissage de la jauge
	jaugeLevel = 0.f;
	recJauge = new osg::Vec3Array(4);
	(*recJauge)[0].set(50.0f,925.,-0.5f);
	(*recJauge)[1].set(50.0f,905.,-0.5f);
	(*recJauge)[2].set(50.0f+jaugeLevel*222.,905.,-0.5f);
	(*recJauge)[3].set(50.0f+jaugeLevel*222.,925.,-0.5f);
	geomJaugeFront = new osg::Geometry(*(drawRectangle(osg::Vec3(50.0f,925.,-0.5f),jaugeLevel*222.,20.,osg::Vec4(1.0f,0.0,0.0f,1.0f))));
	geomJaugeFront->setVertexArray(recJauge);
	m_pgeode->addDrawable(geomJaugeFront);

	// Popup messages
	geomPopup = drawRectangle(osg::Vec3(100,840.,-1.5), 800, 600, osg::Vec4(0.5,0.,0.,0.5));
	popupTxt->setPosition(osg::Vec3(170,670.,-1.));
	popupTxt->setCharacterSize(70);
	m_pgeode->addDrawable(popupTxt);
	
	m_pcam->addChild(m_pgeode);
}


void Hud::initCam(){
	 // create a camera to set up the projection and model view matrices, and the subgraph to draw in the HUD
	 m_pcam = new osg::Camera;

	 // set the projection matrix
	 m_pcam->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));

	 // set the view matrix	 
	 m_pcam->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
	 m_pcam->setViewMatrix(osg::Matrix::identity());

	 // only clear the depth buffer
	 m_pcam->setClearMask(GL_DEPTH_BUFFER_BIT);

	 // draw subgraph after main camera view.
	 m_pcam->setRenderOrder(osg::Camera::POST_RENDER);

	 // we don't want the camera to grab event focus from the viewers main camera(s).
	 m_pcam->setAllowEventFocus(false);
}
osg::ref_ptr<osg::Camera> Hud::getCamHud(){
	return m_pcam;
}

void Hud::drawText(osg::Vec3 pos, char* txt){

	osg::ref_ptr<osgText::Text> text = new osgText::Text;
	m_pgeode->addDrawable( text );

	text->setPosition(pos);
	text->setText(txt);
	text->setCharacterSize(70);
}

osg::ref_ptr<osg::Geometry> Hud::drawRectangle(osg::Vec3 pos, float width, float height, osg::Vec4 color){
	osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;

	// Position du rectangle
	 osg::ref_ptr<osg::Vec3Array> rec = new osg::Vec3Array;
	 rec->push_back(osg::Vec3(pos[0],pos[1],pos[2]));
	 rec->push_back(osg::Vec3(pos[0],pos[1]-height,pos[2]));
	 rec->push_back(osg::Vec3(pos[0]+width,pos[1]-height,pos[2]));
	 rec->push_back(osg::Vec3(pos[0]+width,pos[1],pos[2]));
	 geom->setVertexArray(rec);
 
	// Couleur du rectangle
	 osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
	 colors->push_back(color);
	 geom->setColorArray(colors);
	 geom->setColorBinding(osg::Geometry::BIND_OVERALL);

	 geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));

	 osg::ref_ptr<osg::StateSet> stateset = geom->getOrCreateStateSet();
	 stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
	 stateset->setAttribute(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON);
	 stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

	return geom;
}

void Hud::setJaugeLevel(double level){
	if(jaugeLevel < 1.){ // Increase jauge when detected
		jaugeLevel += level;
		 (*recJauge)[2].set(50.0f+jaugeLevel*222.,905.,-0.5f);
		 (*recJauge)[3].set(50.0f+jaugeLevel*222.,925.,-0.5f);
		 geomJaugeFront->setVertexArray(recJauge);
		if(jaugeLevel>=1){
			endGame("YOU LOOSE! You've been detected!");
		} 
	}
}


void Hud::update(){
	if(jaugeLevel > 0 && jaugeLevel<1){ // Decrease jauge at anytime
		setJaugeLevel(-0.001);
	}
}
void Hud::drawBackground(){
	double width = 222.;
	// TIME
	drawText(osg::Vec3(600.0f,940.,0.0f), (char*)"Time left:");

	// JAUGE
	drawText(osg::Vec3(10.0f,940.,0.0f), (char*)"Suspicious Jauge:");
	m_pgeode->addDrawable(drawRectangle(osg::Vec3(50.0f,925.,0.0f),width,20.,osg::Vec4(0.0f,0.0,1.0f,0.3f)));

	// INVENTORY
	drawText(osg::Vec3(1000,940,0.0f), (char*)"Inventory");	
	m_pgeode->addDrawable(drawRectangle(osg::Vec3(1000,935,-0.5f),width,400.,osg::Vec4(1.0f,1.0,1.0f,0.5f)));

	// MAP
	drawText(osg::Vec3(1000,340.,0.0f), (char*)"Map");
	m_pgeode->addDrawable(drawRectangle(osg::Vec3(1000,325.,-0.5f),width,200.,osg::Vec4(1.0f,1.0,1.0f,0.5f)));
}


//				POPUPS
void Hud::pause(){
	if(Engine::getInstance().getTimer()->isPause()){
		std::cout << "0";
		m_pgeode->addDrawable(geomPopup);
		popupTxt->setText("Pause\nhuhu");
	}else{
		m_pgeode->removeDrawable(geomPopup);
		popupTxt->setText("");
	}
}
void Hud::endGame(std::string msg){
	m_pgeode->addDrawable(geomPopup);
	popupTxt->setText(msg.c_str());
	Engine::getInstance().getTimer()->stop();
}
